package com.test.book.controller;

import com.test.book.model.UserInfo;
import com.test.book.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public boolean login(String name, String password, HttpSession session){
        if (!StringUtils.hasLength(name) || !StringUtils.hasLength(password)){
            return false;
        }
        if (!userService.isCorrectName(name)){
            log.error("用户名错误");
            return false;
        }
        if (userService.selectPasswordByName(name).equals(password)){
            UserInfo userInfo = userService.queryByCondition(name,password);
            session.setAttribute("session_user_key",userInfo);
            return true;
        }
        log.error("密码错误");
       return false;
    }


}
